package com.drugStorage.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.drugStorage.domain.InDrugList;
import com.drugStorage.domain.InStorageBill;
import com.drugStorage.hibernate.Utils.HibernateUtils;
import com.drugStorage.hibernate.dao.InStorageBillDao;

public class InStorageBillDaoImpl implements InStorageBillDao {

	private static final String LOAD_ALL = "from InStorageBill";
	private static final String LOAD_BUYLIST_BY_BUYPLANID = "from InDrugList b where b.inStorageBill.id=?";
	public void DelInDrugList(InDrugList inDrugList) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.delete(inDrugList);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	public void SaveInDrugListById(InDrugList indrugList) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.persist(indrugList);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	public void delInStorageBill(InStorageBill inStorageBill) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.delete(inStorageBill);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}
	}

	public InStorageBill getInStorageBill(int id) {
		Session s = null;

		try {
			s = HibernateUtils.getSession();

			Criteria c = s.createCriteria(InStorageBill.class);
			c.add(Restrictions.eq("id", id));
			InStorageBill inStorageBill = (InStorageBill) c.uniqueResult();
			return inStorageBill;

		} finally {
			if (s != null)
				s.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<InStorageBill> getInStorageBillList() {
		Session s = null;
		try{
			s = HibernateUtils.getSession();
			Query q = s.createQuery(LOAD_ALL);
			return q.list();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	public void saveInStorageBill(InStorageBill inStorageBill) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.persist(inStorageBill);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	@SuppressWarnings("unchecked")
	public List<InDrugList> getInDrugListByInStorageBillid(int id) {
		Session s = null;
		try {
			s = HibernateUtils.getSession();
			Query q = s.createQuery(LOAD_BUYLIST_BY_BUYPLANID);
              q.setParameter(0, id);
              List<InDrugList> l = q.list();
//              for(BuyList b:l){
//              Hibernate.initialize(b.getDrug().getName());
//              }
			return l;

		} finally {
			if (s != null)
				s.close();

		}
	}

}
